package com.think.teaching.controller;


import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.Component;
import com.think.teaching.service.impl.ComponentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 视频课程组件 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@Api(tags =  "视频课程组件接口文档")
public class ComponentController extends BaseController{
	
	private final ComponentServiceImpl componentService;

	public ComponentController(ComponentServiceImpl componentService) {
		this.componentService = componentService;
	}

	@ApiOperation("添加新的视频课程组件")
	@PostMapping("admin/component")
	public Result AddNewComponent(@RequestBody()Component data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			Component component = componentService.AddComponent(data);
			if( component!= null) {
				return Result.success(component);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("更新视频课程组件")
	@RequestMapping(value = "admin/component",method = RequestMethod.PUT)
	public Result UpdateNewComponent(@RequestBody()Component data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			Component component = componentService.UpdateComponent(data);
			if( component!= null) {
				return Result.success(component);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("删除视频课程组件")
	@RequestMapping(value = "admin/component/{id}",method =
			RequestMethod.DELETE)
	public Result DeleteNewComponent(@PathVariable("id")Integer id) {
		Component component = componentService.getById(id);
		if( component== null) {
			return Result.failure(ResultCode.COMPONENT_NOT_EXIST);
		}
		else {
			if (componentService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("获取视频课程组件详情")
	@RequestMapping(value = "admin/component/{id}",method = RequestMethod.GET)
	public Result GetNewComponent(@PathVariable("id")Integer id) {
		Component component = componentService.getById(id);
		if( component== null) {
			return Result.failure(ResultCode.COMPONENT_NOT_EXIST);
		}
		else {
			
			return Result.success(component);
		}
	}
	
	@ApiOperation("管理员修改视频课程组件状态")
	@RequestMapping(value = "admin/component/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateComponentState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		Component component = componentService.getById(id);
		if( component== null) {
			return Result.failure(ResultCode.COMPONENT_NOT_EXIST);
		}
		else {
			try {
				component = componentService.SetComponentState(id, state);
				return Result.success(component);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("用户搜索视频课程组件")
	@RequestMapping(value = "user/component/search",method = RequestMethod.GET)
	public Result UserSearchComponent(@RequestParam("key")String key) {
		return Result.success(componentService.SearchComponentsWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索视频课程组件")
	@RequestMapping(value = "admin/component/search",method = RequestMethod.GET)
	public Result AdminSearchComponent(@RequestParam("key")String key) {
		return Result.success(componentService.SearchComponentsWithKey(key, false));
	}
	
	@ApiOperation("管理员获取视频课程组件列表")
	@RequestMapping(value = "admin/components/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListComponent(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam("tag")Integer tag,
			Integer type) {
		return Result.success(componentService.ListComponentWithTagAndType(count, page, type, tag, false));
	}
	
	@ApiOperation("用户获取视频课程组件列表")
	@RequestMapping(value = "user/components/{page}/{count}",method = RequestMethod.GET)
	public Result UserListComponent(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam("tag")Integer tag,
			Integer type) {
		return Result.success(componentService.ListComponentWithTagAndType(count, page, type, tag, true));
	}
}

